//
//  Order.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/18.
//

#import "Order.h"
#import "OrderDetail.h"
#import "Login.h"
#import "DataFrameWeb.h"
#import "CurrentVC.h"
@interface Order ()
@property(nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIView *scrollContentView;
@end

@implementation Order

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectZero];
    scroll.backgroundColor = UIColor.clearColor;
    [self.view addSubview:scroll];
    [scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.mas_offset(-STATUS_BAR_HEIGHT-MarginTop);
    }];
    scroll.userInteractionEnabled = YES;
    _scrollView = scroll;
    [_scrollView addSubview:self.scrollContentView];
    [self.scrollContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_scrollView);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    UIImageView *top = [UIImageView new];
    top.image = [UIImage imageNamed:@"order_top"];
    [_scrollContentView addSubview:top];
    [top mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_offset(0);
        make.height.mas_equalTo(250);
    }];
    
    UIView *view = [UIView new];
    [_scrollContentView addSubview:view];
    view.backgroundColor = UIColor.clearColor;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(top.mas_bottom).offset(30);
        make.left.mas_offset(13);
        make.right.mas_offset(-13);
        make.bottom.mas_offset(0)
        ;    }];
    
    UIImageView *bg = [UIImageView new];
    bg.image = [UIImage imageNamed:@"order_mid"];
    [view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.mas_offset(-14);
    }];
    
    NSArray *items = @[@"order_btn1",@"order_btn2",@"order_btn3",@"order_btn4",@"order_btn5",@"order_btn6"];
    [items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
        [sender setImage:[UIImage imageNamed:obj] forState:UIControlStateNormal];
        [view addSubview:sender];
        sender.tag = 999+idx;
        [sender mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(16);
            make.right.mas_offset(-16);
            make.height.mas_equalTo(63);
            make.top.mas_offset(63*idx + 14*idx);
            if (idx == 5) {
                make.bottom.mas_offset(-15);
            }
        }];
        [view bringSubviewToFront:sender];
        [sender addTarget:self action:@selector(orderGo:) forControlEvents:UIControlEventTouchUpInside];
    }];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(gotoOrder:)
                                                 name:@"gotoOrder"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(gotoAgree)
                                                 name:@"gotoAgree"
                                               object:nil];

}
-(void)gotoAgree{
    NSInteger index = self.tabBarController.selectedIndex;
    if (index == 1) {
        NSString *privacyAgreement = [[NSUserDefaults standardUserDefaults] objectForKey:@"privacyAgreement"];
        if (![CPS(privacyAgreement) isEqualToString:@""]) {
            DataFrameWeb *web = [DataFrameWeb new];
            web.frameTo = @"https://spi-technologies.com/CPPrivacy.html";
            web.name = @"Privacy Agreement";
            web.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:web animated:YES];
        }
    }
}

- (void)gotoOrder:(NSNotification *)notification {
    NSString *tab = notification.userInfo[@"tab"];
    OrderDetail *vc = [OrderDetail new];
    vc.hidesBottomBarWhenPushed = YES;
    vc.index = [tab integerValue];
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)orderGo:(UIButton *)sender{
    NSString *stewardess = [[NSUserDefaults standardUserDefaults] objectForKey:@"stewardess"];
    if([CPS(stewardess) isEqualToString:@""]){
        Login *login = [Login new];
        [self presentViewController:login animated:YES completion:nil];
        return;
    }

    OrderDetail *vc = [OrderDetail new];
    vc.hidesBottomBarWhenPushed = YES;
    vc.index = sender.tag;
    [self.navigationController pushViewController:vc animated:YES];
}
-(UIView *)scrollContentView{
    if (!_scrollContentView) {
        _scrollContentView = [UIView new];
        _scrollContentView.backgroundColor = UIColor.clearColor;
    }
    return _scrollContentView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
